package jpa.auditing;

import jpa.entity.Account;
import jpa.repository.AccountRepository;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.AuditorAware;

/**
 * Created by barton on 16-5-13.
 */
public class AuditorAwareShiroImpl implements AuditorAware<Account> {

    @Autowired
    private AccountRepository accountRepository;

    @Override
    public Account getCurrentAuditor() {
        // 获得shiro的Subject
        Subject currentUser = SecurityUtils.getSubject();

        // 获得Subject的username
        String username = (String) currentUser.getPrincipal();

        Account account = accountRepository.findByUsername(username);

        return account;
    }
}
